package com.xctech.yace.service;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xctech.yace.util.DateUtil;
import com.xctech.yace.util.DirUtil;
import com.xctech.yace.util.FormatUtil;
import org.apache.kafka.clients.consumer.ConsumerRecord;

import java.io.*;

public class FileService {

    public static String dir = DirUtil.getOrgPath()+"conf/result/";

    public static String jsonDir = DirUtil.getOrgPath()+"conf/json/new/";

    public static String sqldir = DirUtil.getOrgPath()+"conf/gensql/";

    public static void saveFile(String head,ConsumerRecord<String, String> info){
        String value = info.value();
        String topic = info.topic()+"_"+DateUtil.getTodayDate();
        String body = FormatUtil.format(value + "\n");
        FileWriter writer = null;
        BufferedWriter bw = null;
        try{
            String fileName = dir+topic+".txt";
            File file = new File(fileName);
            File fileParent = file.getParentFile();
            if (!fileParent.exists()) {
                fileParent.mkdirs();// 能创建多级目录
            }
            if (!file.exists()) {
                System.out.println("正在创建!");
                try {
                    file.createNewFile();//有路径才能创建文件
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            writer = new FileWriter(fileName,true);
            bw = new BufferedWriter(writer);
            bw.write(head);
            bw.write(body);
            bw.write("=============================================================================");
            bw.newLine();
            bw.flush();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void saveTestDemoFile(ConsumerRecord<String, String> info,String time){
        String value = info.value();
        String topic = info.topic();
//        String body = FormatUtil.format(value + "\n");
        FileWriter writer = null;
        BufferedWriter bw = null;
        try{
            String fileName = dir+"测试案例_"+time+"#"+topic+".sql";
            File file = new File(fileName);
            File fileParent = file.getParentFile();
            if (!fileParent.exists()) {
                fileParent.mkdirs();// 能创建多级目录
            }
            if (!file.exists()) {
                System.out.println("正在创建!");
                try {
                    file.createNewFile();//有路径才能创建文件
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            writer = new FileWriter(fileName,true);
            bw = new BufferedWriter(writer);

            JSONObject jsonObject = null;
            try{
                jsonObject = JSONObject.parseObject(value);
                bw.write(FormatUtil.format(JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue))+"\n");
            }catch (JSONException e1){
                System.out.println("解析json失败,这里做下容错处理,解析下符合hvr合并模式");
                String[] hvrJsons = value.trim().split("\n");
                if(hvrJsons.length == 2){
                    for(int j = 0;j < hvrJsons.length ;j++){
                        jsonObject = JSONObject.parseObject(hvrJsons[j].trim());
                        bw.write(FormatUtil.format(JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue))+"\n");
                        if(j == 0)
                            bw.write("****************************************************"+"\n");
                    }
                }
            }
            bw.write("#################################################################");
            bw.newLine();
            bw.flush();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    public static void saveFile(String sqlfile,String info){

        FileWriter writer = null;
        BufferedWriter bw = null;
        try{
            String fileName = jsonDir+sqlfile;
            File file = new File(fileName);
            File fileParent = file.getParentFile();
            if (!fileParent.exists()) {
                fileParent.mkdirs();// 能创建多级目录
            }
            if (!file.exists()) {
                System.out.println("正在创建!");
                try {
                    file.createNewFile();//有路径才能创建文件
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            writer = new FileWriter(fileName,false);
            bw = new BufferedWriter(writer);
            String[] msgs = info.split("#{5,}");
            for(int i = 0;i < msgs.length ; i++){
                if(i != 0 && !msgs[i].trim().equals("")){
                    bw.write("#####\n");
                }
                bw.write(msgs[i].trim());
                bw.newLine();
            }
            bw.flush();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void saveSqlFile(String sqlfile,String info){

        FileWriter writer = null;
        BufferedWriter bw = null;
        try{
            String fileName = sqldir+sqlfile;
            File file = new File(fileName);
            File fileParent = file.getParentFile();
            if (!fileParent.exists()) {
                fileParent.mkdirs();// 能创建多级目录
            }
            if (!file.exists()) {
                System.out.println("正在创建!");
                try {
                    file.createNewFile();//有路径才能创建文件
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            writer = new FileWriter(fileName,true);
            bw = new BufferedWriter(writer);
//            String[] msgs = info.split("#{5,}");
//            for(int i = 0;i < msgs.length ; i++){
//                if(i != 0 && !msgs[i].trim().equals("")){
//                    bw.write("\n");
//                }
//                bw.write(msgs[i].trim());
//                bw.newLine();
//            }
            bw.write(info);
            bw.newLine();
            bw.flush();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
